namespace nu
{
    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;

    public class SHA1HashProvider : IHashProvider
    {
        private readonly IFileSystem _fileSystem;

        public SHA1HashProvider(IFileSystem file)
        {
            _fileSystem = file;
        }

        #region IHashProvider Members

        public string ComputeHashFor(string filename)
        {
            if(string.IsNullOrEmpty(filename))
            {
                throw new ArgumentNullException("filename");
            }

            if(!_fileSystem.Exists(filename))
            {
                throw new FileNotFoundException("File not found.", filename);
            }

            using(Stream stream = _fileSystem.Read(filename))
            {
                SHA1Managed sha1 = new SHA1Managed();
                byte[] hash = sha1.ComputeHash(stream);
                return Encoding.Default.GetString(hash);
            }
        }

        #endregion
    }
}